package com.bjpowernode.bank.service.impl;

import com.bjpowernode.bank.dao.AccountDao;
import com.bjpowernode.bank.pojo.Account;
import com.bjpowernode.bank.service.AccountService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("accountService")
public class AccountDaoImpl implements AccountService {
    @Resource(name = "accountDao")
    private AccountDao accountDao;
    @Transactional
    @Override
    public void transfer(String fromActno, String toActno, double money) {
        Account fromAccount = accountDao.selectByActno(fromActno);
        if (fromAccount.getBalance() < money){
            throw new RuntimeException("余额不足!!!");
        }
        Account toAccount = accountDao.selectByActno(toActno);

        fromAccount.setBalance(fromAccount.getBalance() - money);
        toAccount.setBalance(toAccount.getBalance() + money);

        int count = accountDao.update(fromAccount);
//        String str = null;
//        str.toString();
        count += accountDao.update(toAccount);
        if(count != 2){
            throw new RuntimeException("转账失败，联系银行");
        }
    }
}
